package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.JsonResult;
import com.hualan.bean.PageResult;
import com.hualan.bean.PanghuShopGoods;
import com.hualan.dao.PanghuGoodsDao;
import com.hualan.dao.PanghuGoodsLike;
import com.hualan.dao.PanghuGoodsLikeImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;

//根据商品名称，门店名称进行模糊查询
@WebServlet("/goods/likeName")
public class PanghuGoodsLikeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("utf-8");

        //获取前端传回的数据
        String goodsName = req.getParameter("goodsName");
        String shopName = req.getParameter("shopName");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String goodsStatus = req.getParameter("goodsStatus");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");

        //新建PanghuGoodsLike对象
        PanghuGoodsLike panghuGoodsLike = new PanghuGoodsLikeImpl();
        // 创建列表存储查询数据
        List<PanghuShopGoods> panghuShopGoods = new ArrayList<>();
        Long total = null; // 总条数

        // 处理分页查询
        if (pageNum == null || "".equals(pageNum)) {
            pageNum = "1";
        }
        if (pageSize == null || "".equals(pageSize)) {
            pageSize = "10";
        }
        // 处理goodsStatus
        if (goodsStatus == null || "".equals(goodsStatus)) {
            goodsStatus = "1";
        }
        System.out.println("后端收到的时间：" + startTime + ", " + endTime);

        //sql语句中 ： 分页  limit 起始位置,条数   （当前页-1）* 每页显示条数    1页
        int pNum = (Integer.valueOf(pageNum) - 1) * Integer.valueOf(pageSize);

        //如果并未输入数据，但点击了搜索
        if ((goodsName == null || "".equals(goodsName)) && (shopName == null || "".equals(shopName)) && (startTime == null || "".equals(startTime)) && (endTime == null || "".equals(endTime))) {

        // 调用dao层查询方法
        panghuShopGoods = panghuGoodsLike.selectShopGoodLike(goodsName, shopName, startTime, endTime, goodsStatus, pNum + "", pageSize);

        //获取总条数
        total = panghuGoodsLike.selectGoodsTotal();
        System.out.println("无搜索参数传入时，" + total + "条数据");
    }else{
        // 调用dao层查询方法
        panghuShopGoods = panghuGoodsLike.selectShopGoodLike(goodsName, shopName, startTime, endTime, goodsStatus, pNum + "", pageSize);

        // 获取查询总条数
        total = Long.valueOf(panghuShopGoods.size());
    }

        System.out.println("long类型total转成int" + total.intValue());

        //整理查询完的总页数，总条数，每页的条数，总数据
        //pageNum，pageSize，total转为int型；每一页的pageSize向上取整；
        PageResult pageResult = new PageResult(Integer.valueOf(pageNum), Integer.valueOf(pageSize), (int) Math.ceil(total.intValue() / Integer.valueOf(pageSize)), total.intValue(), panghuShopGoods);

        // 查询结果
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(pageResult);
        jsonResult.setStatus(200);

        // 处理数据转化为json
        String jsonString = JSON.toJSONString(jsonResult);
        System.out.println("这是转换后要传给前端的json文件" + jsonString);

        //返回数据
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
